// pages/my/my.js
var app = getApp()
Page({
  data: {
    agentId:'',
    accountId:'',
    putInCart:false,
    goodsAttr:[],
    currentItem:'',
    currentIndex:'',
    currentBox:[],
    isStock:false,
    enterCart:true,
    //商品信息
    name:'',imgUrl:'',price:'',sales:'',stock:'',productId:'',goodsType:'',thumbUrl:'',stock_price:'',
    //厂家信息
    brand_url:'',brand_desc:'',brand_name:'',brand:true,
    //配送信息
    logistics:'',
    //商品详情
    details:[],
    num:1,
    actPrice:'',
    totalPrice:'',
    versionValueIds:'',
    ver_value_ids:'',
    typeDetailList:'',
    product_code:''
  },
  onLoad: function (options) {
    let goodsType = options.goodsType,
    goodsId = options.id,
    goodsPrice = options.price
    this.setData({
      price:goodsPrice=="null"?0:goodsPrice,
      totalPrice:goodsPrice,
      productId:goodsId,
      goodsType,
      color:options.color,
      companyName:options.company
    })
    app.getMemInfo().then(res=>{
      let agentId = res.data.agentId
      let accountId = res.data.id
      this.getGoodsDetail(agentId,goodsType,goodsId)
      // this.creatCurrentBox(agentId,goodsType,goodsId)
      this.setData({
        agentId,
        accountId
      })
    })
  },
  bindBuy(){
    this.setData({
      // putInCart:true,
      enterCart:false
    })
  },
  //加入购物车
  bindEnterCart(){
    this.setData({
      // putInCart:true,
      enterCart:true
    })
  },
  bindClose(){
    this.setData({
      putInCart:false
    })
  },
  bindGoToCart(){
    wx.navigateTo({
      url:'replaceSettlement'
    })
  },
  bindChoseAttr(e){
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.name
    let ids = e.currentTarget.dataset.ids
    let currentBox = this.data.currentBox
    const that = this
    currentBox.forEach((item,i)=>{
      if (item.id==id) {
        item.current=true
      }else{
        item.current=false
      }
    })
    console.log(ids)
    this.setData({
      currentBox,
      versionValueIds:id,
      ver_value_ids:ids,
      currentItem:name
    })
    this.getPrice(this.data.versionValueIds)
  },
  //根据商品规格获取库存信息，价格，库存量
  getPrice(skuId){
    // this.getVersions()
    var that = this
    let data = {
      skuId,
      agentId:this.data.agentId
    }
    console.log(data)
    app.request('wx_app/mall/product/version/stockInfo',data,'GET').then(res=>{
      console.log(res)
      that.setData({
        stock:res.data.stock,
        actPrice:res.data.price,
        price:res.data.price,
        thumbUrl:res.data.thumbUrl,
        totalPrice:res.data.price*that.data.num
      })
    })
  },
  getVersions(){
    let versions = []
    let verSort = []
    let versionValueIds =''
    this.data.currentBox.forEach(item=>{
      this.setData({
        versionValueIds:item.id
      })
    })
  },
  //将物品加入购物车
  bindPutInCart(){
    this.setData({
      putInCart:false
    })
    let data = {
      productType:this.data.goodsType,
      mtId:this.data.mtId,
      applyTo:this.data.applyTo,
      accountId:this.data.accountId,
      skuId:this.data.id,
      productName:this.data.name,
      price:this.data.price,
      count:1,
      versionValues:this.data.versionValues,
      versionNameId:this.data.versionNameId,
      isOldForNew:0
    }
    console.log(data)
    app.request('wx_app/oldForNew/cart/add',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        app.errorMsg('加入兑换单成功')
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //结算
  bindToCart(){
    let data = {
      productType:this.data.goodsType,
      mtId:this.data.mtId,
      applyTo:this.data.applyTo,
      accountId:this.data.accountId,
      skuId:this.data.id,
      productName:this.data.name,
      price:this.data.price,
      count:1,
      versionValues:this.data.versionValues,
      versionNameId:this.data.versionNameId,
      isOldForNew:0
    }
    console.log(data)
    app.request('wx_app/oldForNew/cart/add',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        wx.navigateTo({
          url:'replaceSettlement'
        })
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //获得商品详情
  getGoodsDetail(agentId,type,skuId){
    wx.showLoading({
      title:'正在加载'
    })
    var data = {
      agentId,
      type,
      skuId
    }
    var that = this
    app.request('wx_app/mall/product/detail',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        let details = []
        details.push(res.data.sku.imgUrl2,res.data.sku.imgUrl3,res.data.sku.imgUrl4,res.data.sku.imgUrl5,)
        that.setData({
          id:res.data.sku.id,
          mtId:res.data.sku.mtId,
          applyTo:res.data.sku.applyTo,
          name:res.data.sku.productName,
          imgUrl:res.data.sku.mainImgUrl,
          sales:res.data.sku.sales,
          stock:res.data.sku.stock,
          brand_url:res.data.brand_url?res.data.brand_url:'',
          brand_desc:res.data.brand_desc?res.data.brand_desc:'',
          brand_name:res.data.brand_name?res.data.brand_name:'',
          applyTo:res.data.sku.applyTo,
          versionValues:res.data.sku.versionValues,
          versionNameId:res.data.sku.versionNameId,
          details,
          stock_price:res.data.sku.stockPrice,
          product_type:res.data.sku.versionValues+" "+res.data.sku.colorName
      })
        if (!res.data.brand_name) {
          that.setData({
            brand:false
          })
        }else{
          that.setData({
            brand:true
          })
        }
        wx.hideLoading()
      }else{
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
  add(e){
    let num = e.currentTarget.dataset.num
    num++
    let price = this.data.actPrice
    this.setData({
      num,
      totalPrice:this.data.actPrice?this.data.actPrice*num:this.data.price*num
    })
  },
  sub(e){
    let num = e.currentTarget.dataset.num
    num--
    let price = this.data.actPrice

    if (num>0) {
      this.setData({
        num,
        totalPrice:price*num
      })
    }
  },
})